<template>
  <div>
    <van-nav-bar class="login-headers" title="登录">
      <van-icon
        class="rollbock"
        slot="left"
        name="arrow-left"
        @click="$router.back()"
      />
    </van-nav-bar>
    <van-form ref="login_form" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userVali.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="su su-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userVali.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="su su-yanzhengma"></i>
        <template #button>
          <!-- time：时间  finish倒计时结束触发的事件-->
          <van-count-down
            v-if="isDateDis"
            :time="1000 * 30"
            format="ss s"
            @finish="isDateDis = false"
          />
          <van-button
            v-else
            class="login-valid"
            native-type="button"
            round
            size="small"
            type="primary"
            @click="Verification"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, verifi } from "@/api/user";

export default {
  data() {
    return {
      user: {
        mobile: "", //手机号
        code: "", //验证码
      },
      // 验证规则
      userVali: {
        mobile: [
          { required: true, message: "手机号不能为空" },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: "手机号格式有误" },
        ],
        code: [
          { required: true, message: "验证码不能为空" },
          { pattern: /^\d{6}$/, message: "验证码格式有误" },
        ],
      },
      // 是否显示倒计时
      isDateDis: false,
    };
  },
  methods: {
    async onSubmit() {
      // 获取表单数据
      const user = this.user;
      //   在组件中通过this.$toast来调用toast组件
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true, //禁用背景点击
        duration: 0, //持续时间，默认2000，为0 则持续展示
      });
      //   提交表单数据请求登录
      try {
        const { data } = await login(user);
        this.$toast.success("登录成功");
        this.$store.commit("setUser", data.data);
        this.$router.back();
        console.log(data);
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail("手机号或验证码错误");
        } else {
          this.$toast.fail("登录失败，请稍后再试！");
        }
      }
    },
    async Verification() {
      // 验证手机号
      try {
        await this.$refs.login_form.validate("mobile");
      } catch (error) {
        console.log("验证失败", error);
      }
      // 通过后显示倒计时
      this.isDateDis = true;
      // 请求发送验证码
      try {
        await verifi(this.user.mobile);
        this.$toast("验证码已发送，请注意查收！");
      } catch (error) {
        if (error.response.status === 429) {
          this.$toast("请求过于频繁，请稍后再试");
        } else {
          this.$toast("请求失败，请稍后再试");
        }
      }
    },
  },
};
</script>
<style lang="less" scoped>
.van-field {
  .su {
    font-size: 37px;
  }
  .login-valid {
    background-color: #ededed;
    width: 152px;
    height: 46px;
    line-height: 46px;
    font-size: 22px;
    color: #666;
    border: none;
  }
}
.login-btn {
  padding: 53px 33px;
  .van-button {
    background-color: #6db4fb;
    border: none;
  }
}
</style>